#ifndef DEMODULEUTIL_H
#define DEMODULEUTIL_H
#include <QList>
#include "../DemodulatorStruct.h"
#include "../mBPSK_Demodulator/BPSK_Demodulator.h"
#include "../mCommon/MOD.h"
#include "../fftw3.h"
#include "SerialUtil.h"
#include <math.h>
#include <QApplication>


class DemoduleUtil: public QObject
{
    Q_OBJECT
public:
    explicit DemoduleUtil(QObject *parent = nullptr);

    /* 用来记录数据 */
    QVector<double> EqErr_PriCh_Temp; //均衡后误差
    QVector<double> EqSig_PriCh_Temp_I; //DFE均衡后数据-Re
    QVector<double> EqSig_PriCh_Temp_Q; //DFE均衡后数据-Im
    QVector<double> MseqCorrOut_PriCh_Temp; //M序列相关
   // QVector<double> DecodedData_Temp;
    QVector<double> DataPrimary_Temp; //AD采样数据
    QVector<double> DataPrimary_Fre_Temp; //采样数据频谱
    QVector<double> Data_SignalDenoise; //预滤波数据
    PtrProbe   Probe = NULL; //中间数据结构体输入（存储每1秒各模块处理后的数据)
    QList<double> list;
    QVector<double> EqErr_all;
  // double save_data_source[1000];
  // double* tempDataBuffer = NULL;
    int ParityByte_Enable;

    double CourseAngle_fy=0,PitchAngle_fy=0,RollAngle_fy=0; //航向角  俯仰角  工具面角计算结果
    double singnalEfficiency = 0;
    PtrMudSig  MudSigStand = NULL; //原始通道信号输
    void initParam();
    void resetParam();
    void demodule();

    void freeParam();

    void demodule_angle();
    void demodule_angle2();
    void demodule_sendDecData();
    void record_log(QString message);
    int frame_counter = 0;
    QString baseUrl = QApplication::applicationDirPath() + "/conf/";
    int save_source_data; //存储信号源 0不存 1存储原始数据 2存储原始数据与解调后的数据
    double ZoomIn = 1.0; //数据放大倍数
    double sys_zgcd = 9.7; //钻杆长度
signals:
    void sendDeCodeData(QString decData);
    void sendDecCode2WorkPlat(QByteArray decData);
private:
    FIFO_Dec fifoDec;
    FIFO_Dec_Byte fifoDecByte;
    Cout_Dec countDec;
    double tempRead1=0,tempRead2=0;
    FIFO_Receiver fifoReceiver;
     QVector<int> CountN;

   // double* DataPrimary_d = NULL;
    FILE* INIFileStream = NULL;
    FILE* logfile = NULL;    

    PtrDemodulatorStruct  DemodulatorS_PriCh = NULL; //BPSK结构体系数输入
    PtrDemodParam DemodParam = NULL; //解调器参数结构体
    double* DecData = NULL; // 解码后数据 未使用 解码后的数据存储到了Probe (74行)
    double* mseqIndicator = NULL; //M序列信号  未使用

   // double* SigPreFilt_PriCh_buffer = NULL;
   // double* DataPrimary_buffer = NULL;
   // double* SignalDenoise_PriCh_buffer = NULL;
   // double* SigBB_PriCh_buffer = NULL;
   // double* SigBB_Frw_PriCh_buffer = NULL;
   // double* SyncedSigOut_PriCh_buffer = NULL;
   //  double* MseqCorrOut_PriCh_buffer = NULL;
   // int*    FrameSync_PriCh_buffer = NULL;
   // double* CarrierLocI_PriCh_buffer = NULL;
   // double* CarrierLocQ_PriCh_buffer = NULL;
   // double* SigBB_AFC_PriCh_buffer = NULL;
   // double* EqSig_PriCh_buffer = NULL;
   // double* DecData_PriCh_buffer = NULL;
   // double* EqErr_PriCh_buffer = NULL;



    //外部接口参数定义
    int     C_DATA_FRAME_LEN_BIT = 543;		//初始数据帧长度，比特.
    int    PressureSensorNum = 1;			//压力传传感器数量
    unsigned long int ModuleEnableStatus = 0;  //模块开关,注意	 unsigned long int 确保32长

    double  Fc_Hz;// = 125;//载波频率
    double  Rb_bps;// = 25;//数据比特率

    double  Fc_Hz_read;// = 12;//载波频率
    double  Rb_bps_read;// = 3;//数据比特率


   // int     Rb_bps_int = (int)Rb_bps;
    int     Actual_Len;// = Rb_bps_int * SAMPNUM_PERSYMBOL;//采样率变换后信号的实际长度.
    //int     fileseek = 0;
    //int     signal_block_cnt = 0;
    int     s_range = 1; // 处理信号的长度  59


   // double sys_zgcd;    //钻杆长度;

    //FFT转换
    double *fft_in;
    fftw_complex *fft_out;
    fftw_plan my_plan = NULL;



    //角度解码相关参数
    int StateN;
    QList<double> sendDecData;
    int ParityByte_Sum;
    double CourseAngle_Value; //%航相角
    int CourseAngle_Enable;
    double PitchAngle_Value; //俯仰角
    int PitchAngle_Enable;
    double RollAngle_Value; //横滚角
    int RollAngle_Enable;
    double Temperature_Value; //温度
    int Temperature_Enable;
    double ParityByte_Value; //校验字节

};

#endif // DEMODULEUTIL_H
